<?php
/**
 * Created by PhpStorm.
 * User: a4041
 * Date: 2018/6/13
 * Time: 13:46
 */

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * Class OrderReserve
 * @package App\Models
 */
class OrderReserve extends Model
{

    protected $table='order_reserve';

    protected $guarded = ['id', 'created_at', 'updated_at'];

    public function order(){
        return $this->belongsTo(\App\Models\Order::class,'order_id','id');
    }
    protected $appends = ['show_table'];       // 表里没有的字段


    // 难为我了，这样连表，真的醉了
    public function getShowTableAttribute()
    {
        $str='';
        try{
            $desk_room = $this->attributes['desk_room'];  // $this->attributes表的字段属性
            if (strpos($desk_room,'|')){
                $ids = explode('|',$desk_room);
                foreach ($ids as $id){
                    $aas=DB::table('shop_room_tables')->select('name')->find($id);
                    $str.=' '.$aas->name;
                }

            }else{
                $str=DB::table('shop_room_tables')->select('name')->find($desk_room);
                $str = $str->name;
            }
        }catch (\Exception $e){
            $str = '';
        }
        return $str;
    }

//    public function getMyStyleAttribute()
//    {
//        $str = [];
//        try{
//            $desk_room = $this->attributes['desk_room'];  // $this->attributes表的字段属性
//            if (strpos($desk_room,'|')){
//                $ids = explode('|',$desk_room);
//                foreach ($ids as $id){
//                    $aas=DB::table('shop_room_tables')->select('name')->find($id);
//                    $str[]=[
//                        'id'=>$aas->id,
//                        'name'=>$aas->name
//                    ];
////                    $str.=' '.$aas->name;
//                }
//
//            }else{
//                $aas=DB::table('shop_room_tables')->select(['id','name'])->find($desk_room);
//                $str = [
//                    'id'=>$aas->id,
//                    'name'=>$aas->name
//                ];
//            }
//        }catch (\Exception $e){
//            $str = [];
//        }
//
//
//        return $str;
//    }
}